package Search_Thermostat;
import java.util.*;
import java.net.*;
import java.io.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import client.*;

public class client extends TimerTask
{
                static String username = "";
		int set_temp = 0;
		int current_set_temp = 0;
		Search_Thermostat st = new Search_Thermostat();
		String url_thermostat = "";

  double getCurrentTemperature(String urlToRead) {
      URL url;
      HttpURLConnection conn;
      BufferedReader rd;
      String line;
      double result=0.0;
	  DataOutputStream printout;
	  DataInputStream input;
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
		 conn.setDoInput(true);
		 conn.setDoOutput(true);
         conn.setRequestMethod("GET");
		 rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
		 try{
				JSONObject jo = new JSONObject();
				JSONObject temp = jo.fromObject(line);
				result = (Double)temp.get("temp");
				}catch(Exception e){e.printStackTrace();}
         }
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
	String getCurInfo(String urlToRead) {
      URL url;
      HttpURLConnection conn;
      BufferedReader rd;
      String line;
      String result = "";
	  DataOutputStream printout;
	  DataInputStream input;
      try {
         url = new URL(urlToRead);
         conn = (HttpURLConnection) url.openConnection();
		 conn.setDoInput(true);
		 conn.setDoOutput(true);
         conn.setRequestMethod("GET");
		 rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         while ((line = rd.readLine()) != null) {
		 try{
				//System.out.print("Line:"+line);
				set_temp = Integer.parseInt(line);
				}catch(Exception e){e.printStackTrace();}
			result += line;
         }
         rd.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      return result;
   }
	void setParam()
		{
		boolean done = true;
		while(done){
		try {
			// Construct data
			JSONObject j = new JSONObject();
			j.put("t_cool",set_temp);
			String data = j.toString();
			// Send data
			System.out.println(data);
			URL url = new URL("http:/"+url_thermostat+"/tstat");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");
			conn.setRequestProperty("Content-Length", "" + 
			Integer.toString(data.length()));
			conn.setRequestProperty("Content-Language", "en-US");  
			conn.setUseCaches (false);
			conn.setDoInput(true);
      		OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
			wr.write(data);
			wr.flush();
			// Get the response
			BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
			String line;
			while ((line = rd.readLine()) != null) {
				try{
				JSONObject jo = new JSONObject();
				JSONObject temp = jo.fromObject(line);
				//System.out.print(temp.toString());
				}catch(Exception e){}
			}
			wr.close();
			rd.close();
			done = false;
		} catch (Exception e) {
		System.out.println("Exception");
		e.printStackTrace();
		try{
		Thread.sleep(5000);}catch(Exception ee){ee.printStackTrace();}
		
		}
		}
}
public void run()
{
//System.out.println("Recall server");
Search_Thermostat st = new Search_Thermostat();
try{
url_thermostat = st.find();
System.out.println("URL:"+url_thermostat);
}catch(Exception e){
    System.out.println("URL NOT FOUND");
    e.printStackTrace();}
Double s = getCurrentTemperature("http:/"+url_thermostat+"/tstat");
System.out.println("Current Temperature:"+s);

getCurInfo("http://localhost:9999/Thermostat/get_Thermostat_Info?user="+username+"&temp="+Double.toString(s));
setParam();
}
public static void main(String args[]) throws Exception
{
java.util.Timer t1 = new java.util.Timer();
client tt = new client();
if(args.length != 0)
System.out.println("STRING:"+args[0]);
username = args[0];
tt.run();
t1.schedule(tt, 0, 1*60*1000);
}
}